package com.yunmang.fragment;

import android.view.View;
import android.widget.ListView;

import com.yunmang.R;
import com.yunmang.adapter.PiciOrderAdapter;
import com.yunmang.app.AppConstant;
import com.yunmang.entity.BatchDetailBean;
import com.yunmang.http.HttpUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import rx.Subscriber;

/**
 * Created by Administrator on 2017/5/26.
 */
public class PiciListFragment extends BaseFragment {

    @InjectView(R.id.orderListview)
    ListView orderListview;
    private PiciOrderAdapter adapter;
    private List<BatchDetailBean.ModelBean.ItemsBean> dataList;
    private Subscriber<BatchDetailBean> subscriber;
    private String id;
    @Override
    public int getLayoutId() {
        return R.layout.fragment_picilist;
    }

    @Override
    public void initView(View view) {
        ButterKnife.inject(this, view);
    }

    @Override
    public void initData() {
        dataList=new ArrayList<>();
        adapter=new PiciOrderAdapter(getActivity(),dataList);
        orderListview.setAdapter(adapter);
        id=getActivity().getIntent().getStringExtra("batchid");
        requestData(id);
    }

    private void requestData(String bathchId) {
        showWaitingDialog("数据加载中……");
        subscriber=new Subscriber<BatchDetailBean>() {
            @Override
            public void onCompleted() {
                hideWaitingDialog();
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                showToast("获取异常"+e.toString());
            }

            @Override
            public void onNext(BatchDetailBean batchDetailBean) {
                if(batchDetailBean.getCode()==200){
                    dataList=batchDetailBean.getModel().getItems();
                    adapter.setBatchDetailBean(batchDetailBean);
                    adapter.refresh(dataList);
                }else{
                    showToast("获取失败");
                }
            }
        };
        HttpUtils.getInstance().getBatchDetailById(subscriber, AppConstant.userid,Integer.parseInt(bathchId));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
